﻿/*----------------------------------------------------------------
//Copyright (C) 2012 赵国伟
//文件功能描述：出库单管理客户端脚本
//创建人：赵国伟
//生成时间：2012-06-10 16:55:40
//----------------------------------------------------------------*/
var AjaxServerUrl = "/Manage/AS/StoresCommon.aspx";
var ModelOfStockOut;
var ModelOfLine;
var StockOutID = 0;
var SelectOptionOfStores;
var SelectOptionOfStatus;
var TableOfLines = "TableOfLines";
var CurRowOfLines = 0;
var CtrlOfSelGoods;

///初始化
function Init() {
    StockOutID = 0;
    CurRowOfLines = 0;
}

///执行搜索
function GoToSearch() {
    var url = "StockOutsManage.aspx?";
    url += "StockOutSn=" + $("#txtStockOutSnOfSearch").val();
    url += "&StoreID=" + $("#ddlStoreOfSearch").val();
    url += "&StartDate=" + $("#txtStartDateOfSearch").val();
    url += "&EndDate=" + $("#txtEndDateOfSearch").val();
    location.href = url;
}


//执行删除
function Delete(id) {
    if (!confirm("系统提示：您确定要删除该条出库单信息？"))
        return;
    AjaxGet(AjaxServerUrl + "?Action=DeleteStockOut&StockOutID=" + id);
}

///检查表单；
function CheckForm() {
    if ($("#ddlStore").val() == "") {
        alert("系统提示：请选择库房！");
        $("#ddlStore").focus();
        return false;
    }
    if ($("#txtOutDate").val() == "") {
        alert("系统提示：请选择出库日期！");
        $("#txtOutDate").focus();
        return false;
    }
    if ($("#txtOuterName").val() == "") {
        alert("系统提示：请输入出库员！");
        $("#txtOuterName").focus();
        return false;
    }
    if ($("#txtOuterTel").val() == "") {
        alert("系统提示：请输入出库员联系电话！");
        $("#txtOuterTel").focus();
        return false;
    }
    if ($("#txtReceiver").val() == "") {
        alert("系统提示：请输入收货人名称！");
        $("#txtReceiver").focus();
        return false;
    }
    if ($("#txtReceiverTel").val() == "") {
        alert("系统提示：请输入收货人联系电话！");
        $("#txtReceiverTel").focus();
        return false;
    }
    if ($("#txtRecevierAddress").val() == "") {
        alert("系统提示：请输入收货人地址！");
        $("#txtRecevierAddress").focus();
        return false;
    }
    //判断出库清单信息是否填写完整。
    if (CurRowOfLines == 0) {
        alert("系统提示：您还没有设置出库商品！");
        return false;
    }
    for (var i = 0; i < CurRowOfLines; ++i) {
        if ($("#txtQuantityOf" + i) != null) {
            var goodsName = $("#hfGoodsNameOf" + i).val();


            if ($("#txtPriceOf" + i).val() == "" || !isMoney($("#txtPriceOf" + i).val())) {
                alert("系统提示：" + goodsName + " 的单价输入错误！");
                $("#txtPriceOf" + i).focus();
                return false;
            }
            if (parseFloat($("#txtPriceOf" + i).val()) < 0) {
                alert("系统提示：" + goodsName + "的单价不能小于0！");
                $("#txtPriceOf" + i).focus();
                return false;
            }

            if ($("#txtQuantityOf" + i).val()=="" || !IsNumber($("#txtQuantityOf" + i).val())) {
                alert("系统提示：" + goodsName + " 的数量必须为数字！");
                $("#txtQuantityOf" + i).focus();
                return false;
            }
            if (parseInt($("#txtQuantityOf" + i).val(), 10) < 0) {
                alert("系统提示：" + goodsName + "的数量不能小于0！");
                $("#txtQuantityOf" + i).focus();
                return false;
            }

         
        }
    }
    return true;
}

//保存；
function Save() {
    $("#btnSave").attr("disabled", true);
    if (!CheckForm()) {
        $("#btnSave").attr("disabled", false);
        return;
    }

    var data = clone(ModelOfStockOut);
   // data = { "Lines": [], "OutsInfo": { "_createdate": "\/Date(1339326773593+0800)\/", "_outdate": "\/Date(1339326773593+0800)\/", "_outername": null, "_outertel": null, "_receiver": null, "_receiveraddress": null, "_receivertel": null, "_remark": null, "_status": null, "_stockoutid": 0, "_storeid": 0 }, "StoreName": null };
    data.OutsInfo._stockoutid = StockOutID;
    data.OutsInfo._storeid = $("#ddlStore").val();
    data.OutsInfo._outdate = "/Date(" + DateStringToUTC($("#txtOutDate").val()) + ")/";
    data.OutsInfo._outername = $("#txtOuterName").val();
    data.OutsInfo._outertel = $("#txtOuterTel").val();
    data.OutsInfo._receiver = $("#txtReceiver").val();
    data.OutsInfo._receivertel = $("#txtReceiverTel").val();
    data.OutsInfo._receiveraddress = $("#txtRecevierAddress").val();
    data.OutsInfo._status = $("#ddlStatus").val();
    data.OutsInfo._remark = $("#txtRemark").val();
    data.OutsInfo._createdate = "/Date(" + CurrentDateStringToUTC() + ")/";
    //获取出库清单；
    
    for (var i = 0; i < CurRowOfLines; ++i) {
        if ($("#txtQuantityOf" + i) != null) {
            var l = clone(ModelOfLine);
           // l = { "GoodsName": null, "LineInfo": { "_goodsid": 0, "_price": 0, "_quantity": 0, "_stocklineid": 0, "_summoney": 0} };
            l.LineInfo._goodsid = $("#hfGoodsIDOf" + i).val();
            l.LineInfo._price = $("#txtPriceOf" + i).val();
            l.LineInfo._quantity = $("#txtQuantityOf" + i).val();
            l.LineInfo._summoney = parseFloat($("#txtPriceOf" + i).val()).Mul(parseInt(l.LineInfo._quantity, 10));
            l.LineInfo._memo = $("#txtMemoOf" + i).val();
            l.GoodsName = $("#hfGoodsNameOf" + i).val();
            data.Lines.push(l);
        }
    }


    var url = AjaxServerUrl + "?Action=SaveStockOutInfo";
    AjaxPost(url, "btnSave", data);  //更新
}

//绑定出库单信息到录入表单中；
function BindStockOutToForm() {
    var url = AjaxServerUrl + "?action=GetStockOutInfo&StockOutID=" + StockOutID + "&tttt=" + Math.random();
    $.ajax(
    {
        type: "Get",
        url: url,
        dataType: "json",
        success: function (data) {
            if (data != null) {
                $("#spStockOutID").html(data.OutsInfo._stockoutid);
                $("#ddlStore").val(data.OutsInfo._storeid);
                $("#txtOutDate").val(ConvertJsonDatetoToLocateDate(data.OutsInfo._outdate));
                $("#txtOuterName").val(data.OutsInfo._outername);
                $("#txtOuterTel").val(data.OutsInfo._outertel);
                $("#txtReceiver").val(data.OutsInfo._receiver);
                $("#txtReceiverTel").val(data.OutsInfo._receivertel);
                $("#txtRecevierAddress").val(data.OutsInfo._receiveraddress);
                $("#ddlStatus").val(data.OutsInfo._status);
                $("#txtRemark").val(data.OutsInfo._remark);
                //输出出库清单；
                for (var i = 0; i < data.Lines.length; ++i) {
                    var l = data.Lines[i];
                    AddLine(l.LineInfo._goodsid, l.GoodsName, 0, l.PackUnit);
                    $("#txtPriceOf" + i).val(l.LineInfo._price);
                    $("#txtQuantityOf" + i).val(l.LineInfo._quantity);
                   
                    $("#txtMemoOf" + i).val(l.LineInfo._memo);
                }


            }
        }
    });
}

///添加界面；
function Add() {
    Init();
    GetPopupDiv(700, 800, "#e2e2e2", "添加出库单", GetHtmlForm());
    CtrlOfSelGoods = new SelectGoods('btnSelGoods', 'Single', null, null, AddLine);
}

//编辑界面；
function Edit(id) {
    Init();
    StockOutID = id;
    GetPopupDiv(700, 800, "#e2e2e2", "编辑出库单", GetHtmlForm());
    CtrlOfSelGoods = new SelectGoods('btnSelGoods', 'Single', null, null, AddLine);
    BindStockOutToForm();
}

///获取录入界面的HTML;
function GetHtmlForm() {
    var html = new Array();
    html.push("<form>");
    html.push("<table width=\"850\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"table\">");
    html.push("  <tr>");
    html.push("    <td colspan=\"4\" class=\"HeadTitle\">&gt;&gt;出库单基本信息</td>");
    html.push("  </tr>");

    
    html.push("  <tr>");
    html.push("    <td width=\"123\" align=\"right\">出库单号：</td>");
    html.push("    <td colspan=\"3\"><span id=\"spStockOutID\">");
    html.push("</span></td>");
    html.push("    </tr>");

    html.push("  <tr>");
    html.push("    <td align=\"right\">库房：</td>");
    html.push("    <td colspan=\"3\"><select name=\"ddlStore\" id=\"ddlStore\">");
    html.push("      <option value=\"\">--请选择库房--</option>");
    html.push(SelectOptionOfStores);
    html.push("    </select><span class=\"red\">(*)</span></td>");
    html.push("    </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">出库日期：</td>");
    html.push("    <td colspan=\"3\"><input name=\"txtOutDate\" type=\"text\" id=\"txtOutDate\" size=\"12\" class=\"Wdate\" readonly=\"readonly\" onfocus=\"new WdatePicker({el:this})\" style=\"cursor: hand\"/><span class=\"red\">(*)</span></td>");
    html.push("    </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">出库员：</td>");
    html.push("    <td width=\"281\"><input name=\"txtOuterName\" type=\"text\" id=\"txtOuterName\" size=\"20\" class=\"border6\"  /><span class=\"red\">(*)</span></td>");
    html.push("    <td width=\"131\" align=\"right\">出库人联系电话：</td>");
    html.push("    <td width=\"315\"><input name=\"txtOuterTel\" type=\"text\" id=\"txtOuterTel\" size=\"20\" class=\"border6\" /><span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">收货人：</td>");
    html.push("    <td><input name=\"txtReceiver\" type=\"text\" id=\"txtReceiver\" size=\"20\" class=\"border6\" /><span class=\"red\">(*)</span></td>");
    html.push("    <td align=\"right\">收货人联系电话：</td>");
    html.push("    <td><input name=\"txtReceiverTel\" type=\"text\" id=\"txtReceiverTel\" size=\"20\" class=\"border6\" /><span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">收货人地址：</td>");
    html.push("    <td colspan=\"3\"><input name=\"txtRecevierAddress\" type=\"text\" id=\"txtRecevierAddress\" size=\"40\" class=\"border6\" /><span class=\"red\">(*)</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">出库状态：</td>");
    html.push("    <td colspan=\"3\"><select name=\"ddlStatus\" id=\"ddlStatus\">");
    html.push(SelectOptionOfStatus);
    html.push("      </select>      </td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">备注：</td>");
    html.push("    <td colspan=\"3\"><textarea name=\"txtRemark\" cols=\"70\" rows=\"3\" id=\"txtRemark\"></textarea></td>");
    html.push("    </tr>");
    html.push("  <tr>");
    html.push("    <td colspan=\"4\" align=\"center\"><input name=\"btnSave\" type=\"button\" id=\"btnSave\" value=\" 保 存 \" class=\"btn2\" onclick=\"Save();\">");
    html.push("      <input name=\"btnReset\" type=\"reset\" id=\"btnReset\" value=\" 重 置 \" class=\"btn2\">");
    html.push("      <input name=\"btnClose\" type=\"button\" id=\"btnClose\" value=\"关闭本窗口\" class=\"btn2\" onclick=\"yesOther(false);\"></td>");
    html.push("    </tr>");
    html.push("</table>");
    html.push("<table width=\"850\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"table\" id=\"TableOfLines\">");
    html.push("  <tr>");
    html.push("    <td colspan=\"8\" class=\"HeadTitle\">&gt;&gt;商品明细 <input name=\"btnSelGoods\" type=\"button\" id=\"btnSelGoods\" value=\"选择商品..\" class=\"btn2\"  /> </td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"center\">编号</td>");
    html.push("    <td align=\"center\">商品名称</td>");
    html.push("    <td align=\"center\">单位</td>");
    html.push("    <td align=\"center\">单价</td>");
    html.push("    <td align=\"center\">数量</td>");
    html.push("    <td align=\"center\">金额</td>");
    html.push("    <td align=\"center\">备注</td>");
    html.push("    <td align=\"center\">操作</td>");
    html.push("    </tr>");

    html.push("<tr id=\"trNoLine\">");
    html.push("<td colspan=\"8\" class=\"red\" align=\"center\">[ 还没有添加任何商品！ ]</td>");
    html.push("</tr>");
    
    html.push("  <tr>");
    html.push("    <td colspan=\"4\" align=\"right\">&nbsp;</td>");
    html.push("    <td align=\"center\"><span id=\"spTotalQuantity\">0</span></td>");
    html.push("    <td align=\"center\">￥<span id=\"spTotalMoney\">0</span></td>");
    html.push("    <td align=\"center\">&nbsp;</td>");
    html.push("    <td align=\"center\">&nbsp;</td>");
    html.push("    </tr>");
    html.push("</table>");
    html.push("</form>");

    return html.join("");

}



function UpdateLineCounts(i) {
    var t = parseInt($("#txtQuantityOf" + i).val(), 10);
    $("#spLineCountsOf" + i).html(parseFloat($("#txtPriceOf" + i).val()).Mul(t));
    UpdateCounts();

}

///更新统计信息；
function UpdateCounts() {
    var totalQuantity = 0;
    var totalMoney = 0.00;
    for (var i = 0; i < CurRowOfLines; ++i) {
        if ($("#txtQuantityOf" + i) != null && $("#txtQuantityOf" + i) != undefined) {
            var t = parseInt($("#txtQuantityOf" + i).val(), 10);
            totalQuantity += t;
            var s = parseFloat($("#txtPriceOf" + i).val());
            totalMoney += s.Mul(t);
        }
    }
    $("#spTotalQuantity").html(totalQuantity);
    $("#spTotalMoney").html(totalMoney);

}


function AddLine(goodsId,goodsName,saleprice,packUnit) {

    $("#trNoLine").hide();
    var tr = oo(TableOfLines).insertRow(oo(TableOfLines).rows.length-1);
    td = tr.insertCell(0);
    td.innerHTML = ((CurRowOfLines + 1) + ".");  //序号列；
    td.align = "center";

    td = tr.insertCell(1);
    td.innerHTML = goodsName + "<input type=\"hidden\" name=\"hfGoodsIDOf" + CurRowOfLines + "\" id=\"hfGoodsIDOf" + CurRowOfLines + "\" value=\"" + goodsId + "\" />" + "<input type=\"hidden\" name=\"hfGoodsNameOf" + CurRowOfLines + "\" id=\"hfGoodsNameOf" + CurRowOfLines + "\" value=\"" + goodsName + "\" />";
    td.align = "center";

    td = tr.insertCell(2);
    td.innerHTML = packUnit;
    td.align = "center";

    td = tr.insertCell(3);
    td.innerHTML = "<input name=\"txtPriceOf" + CurRowOfLines + "\" type=\"text\" id=\"txtPriceOf" + CurRowOfLines + "\" size=\"12\" class=\"border6\" value=\"0\" onblur=\"UpdateLineCounts(" + CurRowOfLines + ")\" />";
    td.align = "center";

    td = tr.insertCell(4);
    td.innerHTML = "<input name=\"txtQuantityOf" + CurRowOfLines + "\" type=\"text\" id=\"txtQuantityOf" + CurRowOfLines + "\" size=\"12\" class=\"border6\" value=\"1\" onblur=\"UpdateLineCounts(" + CurRowOfLines + ")\" />";
    td.align = "center";

    td = tr.insertCell(5);
    td.innerHTML = "￥<span id=\"spLineCountsOf" + CurRowOfLines + "\">0</span>";
    td.align = "center";

    td = tr.insertCell(6);
    td.innerHTML = "<input name=\"txtMemoOf" + CurRowOfLines + "\" type=\"text\" id=\"txtMemoOf" + CurRowOfLines + "\" size=\"12\" class=\"border6\" />";
    td.align = "center";


    td = tr.insertCell(7);
    td.innerHTML = "[ <a href=\"javascript:void(0);\" onclick=\"javascript:DeleteLine(this);\" >删除</a> ]";
    td.align = "center";
    CurRowOfLines++;
    UpdateLineCounts(CurRowOfLines - 1);
}

function DeleteLine(objRow) {
    if (!confirm("确定要删除该行吗?"))
        return false;

    var rowIndex = objRow.parentNode.parentNode.rowIndex;
    CurRowOfLines--;
    UpdateCounts();
    oo(TableOfLines).deleteRow(rowIndex);
    if (oo(TableOfLines).rows.length == 4) {
        CurRowOfLines = 0;
        $("#trNoLine").show();
    }

}













